//
//  DMForgetViewController.m
//  DMGME
//
//  Created by MC on 15/10/28.
//  Copyright © 2015年 GYP. All rights reserved.
//

#import "DMForgetViewController.h"
#import "DMRegistAController.h"
@interface DMForgetViewController ()<UIAlertViewDelegate>
{
    UILabel *_lableTime;
    NSTimer *_timer;
    int seconds;
    NSDictionary *codeDic;
}
@end

@implementation DMForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
       [self setTitle:@"找回密码"];
    self.view.backgroundColor = [UIColor whiteColor];
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    codeDic = [NSDictionary dictionary];
    [self createUI];
}
-(void)createUI
{
    UILabel *phoneNo = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, KWidth/2-20, 21)];
    
    phoneNo.textColor = [UIColor blackColor];
    
    phoneNo.font = [UIFont boldSystemFontOfSize:13];
    
    phoneNo.text = @"请输入您的手机号:";
    
    
    [self.view addSubview:phoneNo];
    
    UITextField *phoneText = [[UITextField alloc] init];
    
    phoneText.frame = CGRectMake(40, CGRectGetMaxY(phoneNo.frame)+10, KWidth-80, 40);
    
    phoneText.borderStyle = UITextBorderStyleBezel;
    
    phoneText.inputAccessoryView = [self GetInputAccessoryView];
    
    phoneText.keyboardType = UIKeyboardTypeNumberPad;
    
    phoneText.tag = 10;
    
    [self.view addSubview:phoneText];
    
    UILabel *yanzhnengLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(phoneText.frame)+10, phoneText.frame.size.width, 21)];
    
    yanzhnengLabel.textColor = [UIColor blackColor];
    
    yanzhnengLabel.font = [UIFont boldSystemFontOfSize:13];
    
    yanzhnengLabel.text = @"请输入手机验证码:";
    
    [self.view addSubview:yanzhnengLabel];
    
    UITextField *yanzhengText = [[UITextField alloc] initWithFrame:CGRectMake(phoneText.frame.origin.x, CGRectGetMaxY(yanzhnengLabel.frame)+10, phoneText.frame.size.width/2, 40)];
    
    yanzhengText.borderStyle = UITextBorderStyleBezel;

    yanzhengText.inputAccessoryView = [self GetInputAccessoryView];
    yanzhengText.keyboardType = UIKeyboardTypeNumberPad;
    yanzhengText.tag = 121;
    [self.view addSubview:yanzhengText];
    
    
    UIButton *getCode = [UIButton buttonWithType:UIButtonTypeCustom];
    
    getCode.tag = 20;
    
    getCode.frame = CGRectMake(CGRectGetMaxX(yanzhengText.frame)+10, CGRectGetMaxY(yanzhnengLabel.frame)+10, phoneText.frame.size.width/2-20, 40);
    
    [getCode setBackgroundImage:[UIImage imageNamed:@"getPhoneCodeButton"] forState:UIControlStateNormal];
    
    _lableTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, getCode.frame.size.width, getCode.frame.size.height)];
    //    _getCodeNumBtn.enabled = NO;
    
    [_lableTime setTextAlignment:NSTextAlignmentCenter];
    
    [getCode addSubview:_lableTime];
    
    [getCode addTarget:self action:@selector(getCodeNumBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:getCode];
    
    UILabel *newPaddwd = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(yanzhengText.frame)+10, phoneText.frame.size.width, 21)];
    
    newPaddwd.textColor = [UIColor blackColor];
    
    newPaddwd.font = [UIFont boldSystemFontOfSize:13];
    
    newPaddwd.text = @"请输入新密码:";
    
    
    [self.view addSubview:newPaddwd];
    
 
    
    UITextField *newPasswdText = [[UITextField alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(newPaddwd.frame)+10, KWidth-80, 40)];
    
    newPasswdText.borderStyle = UITextBorderStyleBezel;

    newPasswdText.inputAccessoryView = [self GetInputAccessoryView];

    newPasswdText.tag = 125;
    
    newPasswdText.secureTextEntry = YES;
    
    [self.view addSubview:newPasswdText];
    
    
    UIButton *lookForBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    lookForBtn.frame = CGRectMake(42, CGRectGetMaxY(newPasswdText.frame)+30, KWidth-84, 45);
    
    [lookForBtn setBackgroundImage:[UIImage imageNamed:@"FindPassword"] forState:UIControlStateNormal];
    [lookForBtn addTarget:self action:@selector(askForPasswd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lookForBtn];
    
    UIImageView *redLine = [[UIImageView alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(lookForBtn.frame)+25, KWidth-10, 0.5)];
    redLine.backgroundColor = ComColor;
    
    [self.view addSubview:redLine];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    registerBtn.frame = CGRectMake(50, CGRectGetMaxY(redLine.frame)+5, KWidth-100, 45);
    
    [registerBtn setImage:[UIImage imageNamed:@"SignButton"] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(registBtnLogInClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:registerBtn];
}
//注册
- (void)registBtnLogInClick{
    DMRegistAController *regiVC = [[DMRegistAController alloc]initWithNibName:@"DMRegistAController" bundle:nil];
    [self.navigationController pushViewController:regiVC animated:YES];
}
//验证码
- (void)getCodeNumBtnClick
{
    UITextField *phonrText = (UITextField *)[self.view viewWithTag:10];
    if (!phonrText.text || phonrText.text.length == 0) {
        [self showMsg:@"请输入手机号"];
    }else{
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
        seconds = 60;
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$captcha"] forKey:@"token"];
        [dict setValue: phonrText.text forKey:@"mobile"];
        [dict setValue: @"captcha" forKey:@"action"];
        
        [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//            NSLog(@"注册 == %@",responseObject);
            
            if ([responseObject[@"code"] intValue] == 0) {
                
                codeDic = responseObject[@"data"];
            }
            
//                       NSLog(@"%@",responseObject[@"desc"]);
            
        } failure:^(NSError *error) {
        }];
    }
    
}
-(void)timerFireMethod:(NSTimer *)theTimer {
    
    UIButton *getCode = (UIButton *)[self.view viewWithTag:20];
    
    if (seconds == 0) {
        [theTimer invalidate];
        [_lableTime setText:@""];
        [getCode setBackgroundImage:[UIImage imageNamed:@"getPhoneCodeButton"] forState:UIControlStateSelected];
        [getCode setEnabled:YES];
    }else{
        seconds--;
        [getCode setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [getCode setEnabled:NO];
        
        [getCode setBackgroundImage:[UIImage imageNamed:@"15@2x_13"] forState:UIControlStateDisabled];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_lableTime setText:[NSString stringWithFormat:@"%d秒",seconds]];
        });
    }
}
-(void)askForPasswd
{
    if (codeDic[@"agent"] == [NSNull null] ) {
        [self showMsg:@"你的手机号尚未注册"];
        return;
    }
    else
    {
        UITextField *phonrText = (UITextField *)[self.view viewWithTag:10];
        UITextField *passdText = (UITextField *)[self.view viewWithTag:125];
        UITextField *yanzhengText = (UITextField *)[self.view viewWithTag:121];
        if (passdText.text.length == 0)
        {
            [self showMsg:@"密码不能为空"];
        }
        else if(yanzhengText.text.length == 0)
        {
            [self showMsg:@"请输入验证码"];
        }
        else
        {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$getPwd"] forKey:@"token"];
        [dict setValue: codeDic[@"agent"][@"id"] forKey:@"id"];
        [dict setValue: phonrText.text forKey:@"mobile"];
        [dict setValue: @"getPwd" forKey:@"action"];
        [dict setValue: passdText.text forKey:@"password"];
        [dict setValue: yanzhengText.text forKey:@"captcha"];
        [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//            NSLog(@"=== %@",REGISTERNET);
//            NSLog(@"==== %@",dict);
//            NSLog(@"找回密码 == %@",responseObject);
            
            if ([responseObject[@"desc"] length] == 0) {
                
                UIAlertView *ater = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改成功，返回登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [ater show];
            
        
                           }
//            sleep(3);
//            if ([responseObject[@"desc"] length] == 0) {
//                [self.navigationController popToRootViewControllerAnimated:YES];
//            }
            
        } failure:^(NSError *error) {
        }];
       
       
            }
        }
   
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
